<?php

/**
 * This is the model class for table "lths_user".
 *
 * The followings are the available columns in table 'lths_user':
 * @property integer $id
 * @property string $staff_id
 * @property string $user_name
 * @property string $depart_code
 * @property string $eparchy_code
 * @property string $phone
 * @property string $mail
 * @property string $password
 * @property integer $post_code
 * @property integer $state_code
 * @property string $role
 * @property string $create_time
 * @property string $update_time
 *
 * The followings are the available model relations:
 * @property Eparchy $eparchyCode
 * @property Post $postCode
 * @property Userstate $stateCode
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'lths_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('staff_id, user_name, depart_code, phone, mail, post_code, password, role', 'required'),
			array('post_code, state_code', 'numerical', 'integerOnly'=>true),
			array('staff_id, depart_code, phone', 'length', 'max'=>20),
			array('user_name, password, mail', 'length', 'max'=>50),
			array('eparchy_code', 'length', 'max'=>4),
			array('create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, staff_id, user_name, depart_code, eparchy_code, phone, mail, password, post_code, state_code, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'eparchyCode' => array(self::BELONGS_TO, 'Eparchy', 'eparchy_code'),
			'departCode' => array(self::BELONGS_TO, 'Depart', 'depart_code'),
			'postCode' => array(self::BELONGS_TO, 'Post', 'post_code'),
			'stateCode' => array(self::BELONGS_TO, 'Userstate', 'state_code'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'staff_id' => '工号',
			'user_name' => '姓名',
			'depart_code' => '归属部门',
			'eparchy_code' => '地市',
			'phone' => '手机号码',
			'mail' => '邮箱',
			'password' => '密码',
			'post_code' => '职位',
			'state_code' => '目前状态',
			'role' => '权限组',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
		);
	}
	public function findByStaffId($staff_id)
	{
		return $this->findByAttributes(array('staff_id' => $staff_id,'state_code'=>0,));
	}
	
	public function getListData($staff_id)
	{
		$results = array();
		$staffs = $this->findAll();
	    foreach ($staffs as $key => $value) {
	    	$results[$value->staff_id] = $value->user_name;
	    }
	    return $results;
	}
	
	public function getListDataInDepart($staff_id)
	{
		$results = array();
		$curStaff = $this->findByAttributes(array('staff_id'=>$staff_id));
		$staffs = $this->findAllByAttributes(array('depart_code'=>$curStaff->depart_code));
		foreach ($staffs as $key => $value) {
	    	$results[$value->staff_id] = $value->user_name;
	    }
	    return $results;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('staff_id',$this->staff_id,true);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('depart_code',$this->depart_code,true);
		$criteria->compare('eparchy_code',$this->eparchy_code,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('mail',$this->mail,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('post_code',$this->post_code);
		$criteria->compare('state_code',$this->state_code);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function onAfterSave($event)
	{
		//Yii::log('onAfterSave User','info');
		Authassignment::model()->assignRole($this->staff_id,$this->role);
		
	}
	public function behaviors(){  
    return array(  
        'CTimestampBehavior' => array(  
            'class' => 'zii.behaviors.CTimestampBehavior',  
            'createAttribute' => 'create_time',  
            'updateAttribute' => 'update_time',  
        )  
      );  
    }  
}